home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / netz / term / extras / source / term-source.lha / termSpeech.c < prev    next >
C/C++ Source or Header  |  1995-02-07  |  3KB  |  169 lines

  1. /*
  2. **    termSpeech.c
  3. **
  4. **    Speech support routines
  5. **
  6. **    Copyright © 1990-1995 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     /* Local symbols. */
  13.  
  14. STATIC struct MsgPort        *NarratorPort;
  15. STATIC struct narrator_rb    *NarratorRequest;
  16. STATIC UBYTE __far         SpeechString[512],
  17.                  TranslatedString[512];
  18. STATIC BYTE             DidSpeak;
  19.  
  20.     /* DeleteSpeech():
  21.      *
  22.      *    Free the data associated with the speech function.
  23.      */
  24.  
  25. VOID
  26. DeleteSpeech()
  27. {
  28.     if(DidSpeak)
  29.     {
  30.         if(!CheckIO(NarratorRequest))
  31.             WaitIO(NarratorRequest);
  32.     }
  33.  
  34.     if(NarratorRequest)
  35.     {
  36.         if(NarratorRequest -> message . io_Device)
  37.             CloseDevice(NarratorRequest);
  38.  
  39.         DeleteIORequest(NarratorRequest);
  40.  
  41.         NarratorRequest = NULL;
  42.     }
  43.  
  44.     if(NarratorPort)
  45.     {
  46.         DeleteMsgPort(NarratorPort);
  47.  
  48.         NarratorPort = NULL;
  49.     }
  50.  
  51.     if(TranslatorBase)
  52.     {
  53.         CloseLibrary(TranslatorBase);
  54.  
  55.         TranslatorBase = NULL;
  56.     }
  57.  
  58.     DidSpeak = FALSE;
  59. }
  60.  
  61.     /* CreateSpeech():
  62.      *
  63.      *    Open translator.library and narrator.device, perform
  64.      *    the necessary setups for the speech function.
  65.      */
  66.  
  67. BYTE
  68. CreateSpeech()
  69. {
  70.     if(TranslatorBase = OpenLibrary("translator.library",0))
  71.     {
  72.         if(NarratorPort = CreateMsgPort())
  73.         {
  74.             if(NarratorRequest = (struct narrator_rb *)CreateIORequest(NarratorPort,sizeof(struct narrator_rb)))
  75.             {
  76.                 STATIC UBYTE AnyChannel[] =
  77.                 {
  78.                     LEFT0F,
  79.                     LEFT1F,
  80.                     RIGHT0F,
  81.                     RIGHT1F
  82.                 };
  83.  
  84.                     /* Any channel will do. */
  85.  
  86.                 NarratorRequest -> ch_masks        = AnyChannel;
  87.                 NarratorRequest -> nm_masks        = sizeof(AnyChannel);
  88.  
  89.                     /* This is a write request. */
  90.  
  91.                 NarratorRequest -> message . io_Command    = CMD_WRITE;
  92.                 NarratorRequest -> message . io_Data    = (APTR)SpeechString;
  93.  
  94.                 if(!OpenDevice("narrator.device",0,NarratorRequest,0))
  95.                 {
  96.                     SpeechSetup();
  97.  
  98.                     return(TRUE);
  99.                 }
  100.             }
  101.         }
  102.     }
  103.  
  104.     DeleteSpeech();
  105.  
  106.     return(FALSE);
  107. }
  108.  
  109.     /* Say(STRPTR String,...):
  110.      *
  111.      *    Translate a string into phonemes and speak it.
  112.      */
  113.  
  114. VOID __stdargs
  115. Say(STRPTR String,...)
  116. {
  117.     if(SpeechConfig . Enabled && English)
  118.     {
  119.         if(!TranslatorBase)
  120.             CreateSpeech();
  121.  
  122.         if(TranslatorBase)
  123.         {
  124.             va_list VarArgs;
  125.  
  126.             if(DidSpeak)
  127.             {
  128.                 if(!CheckIO(NarratorRequest))
  129.                     WaitIO(NarratorRequest);
  130.             }
  131.  
  132.             va_start(VarArgs,String);
  133.             VSPrintf(TranslatedString,String,VarArgs);
  134.             va_end(VarArgs);
  135.  
  136.             if(!Translate(TranslatedString,strlen(TranslatedString),SpeechString,511))
  137.             {
  138.                 NarratorRequest -> message . io_Length = strlen(SpeechString);
  139.  
  140.                 ClrSignal(PORTMASK(NarratorRequest -> message . io_Message . mn_ReplyPort));
  141.  
  142.                 SendIO(NarratorRequest);
  143.  
  144.                 DidSpeak = TRUE;
  145.             }
  146.             else
  147.                 DidSpeak = FALSE;
  148.         }
  149.     }
  150. }
  151.  
  152.     /* SpeechSetup():
  153.      *
  154.      *    Transfer the configuration data into the setup.
  155.      */
  156.  
  157. VOID
  158. SpeechSetup()
  159. {
  160.     if(NarratorRequest)
  161.     {
  162.         NarratorRequest -> rate        = SpeechConfig . Rate;
  163.         NarratorRequest -> pitch    = SpeechConfig . Pitch;
  164.         NarratorRequest -> sex        = SpeechConfig . Sex;
  165.         NarratorRequest -> volume    = SpeechConfig . Volume;
  166.         NarratorRequest -> sampfreq    = SpeechConfig . Frequency;
  167.     }
  168. }
  169.